<?php

namespace app\common\model\Business;

use think\Model;
use traits\model\SoftDelete;

/**
 * 客户模型
 */
class Business extends Model
{
    //让当前模型 具备软删除功能
    use SoftDelete;

    // 表名
    protected $name = 'business';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名 以下的名字 必须要在数据表里面有这个字段
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';
    
    // 忽略数据表不存在的字段
    protected $field = true;
    //追加自定义内容
    protected $append = [
        'avatar_text',
        'nickname_text',
        'gender_text',
        'region_code',
        'region_text'
    ];
    
    //增加一个获取器的方法 get获取器的名字Attr(字段值, 整条数据的结果)
    public function getAvatarTextAttr($value, $data)
    {
        //域名地址
        $domian = empty(config('url_domain_root')) ? "http://www.fastadmin.com/" : trim(config('url_domain_root'));
        $domian = trim($domian, "/");

        // 默认图片
        $default = '/assets/img/avatar.png';

        //先获取字段值
        $avatar = isset($data['avatar']) ? trim($data['avatar']) : $default;
        $avatar = empty($avatar) ? $default : trim($avatar);

        //判断图片是否真实存在
        $avatar = is_file("." . $avatar) ? $avatar : $default;
        $avatar = $domian.$avatar;
        return $avatar;
    }

    public function getNicknameTextAttr($value, $data)
    {
        $default = '暂无昵称';
        $nickname = isset($data['nickname']) ? $data['nickname'] : $default;
        $nickname = empty($nickname) ? $default : trim($nickname);
        return $nickname;
    }

    public function getGenderTextAttr($value,$data)
    {
        $gender=isset($data['gender']) ? $data['gender'] : '0';
        $text = '';
        switch($gender)
        {
            case "0":
                $text = '保密';
                break;
            case "1":
                $text = '男';
                break;
            case "2":
                $text = '女';
                break;
        }
        return $text;
    }

    //最深的地区码
    public function getRegionCodeAttr($value, $data)
    {
        $province = isset($data['province']) ? $data['province'] : '';
        $city = isset($data['city']) ? $data['city'] : '';
        $district = isset($data['district']) ? $data['district'] : '';

        $code = '';

        if(!empty($province))
        {
            $code = $province;
        }

        if(!empty($city))
        {
            $code = $city;
        }

        if(!empty($district))
        {
            $code = $district;
        }

        return $code;
    }


    //综合地区文字
    public function getRegionTextAttr($value, $data)
    {
        $province = empty($data['province']) ? "0" : $data['province'];
        $city = empty($data['city']) ? "0" : $data['city'];
        $district = empty($data['district']) ? "0" : $data['district'];

        $region = '';

        $province_name = model('Region')->where(['code' => $province])->value('name');
        $city_name = model('Region')->where(['code' => $city])->value('name');
        $district_name = model('Region')->where(['code' => $district])->value('name');

        if(!is_null($province_name))
        {
            $region .= $province_name;
        }

        if(!is_null($city_name))
        {
            $region .= "/".$city_name;
        }

        if(!is_null($district_name))
        {
            $region .= "/".$district_name;
        }

        return $region;
    }

    //关联查询
    public function source()
    {
        return $this->belongsTo('Source', 'sourceid', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    //关联查询
    public function admin()
    {
        return $this->belongsTo('app\common\model\Admin', 'adminid', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function region()
    {
        return $this->belongsTo('app\common\model\Region', 'district', 'code', [], 'LEFT')->setEagerlyType(0);
    }

}
